// filename: XWidget.h
// author: enic
// date: 2016-03-29

#pragma once

#include "import_cocos.h"
#include "import_XLib.h"
#include "XWidget.h"
#include "XWidgetPropertyUtility.h"

namespace X
{
	XWidget::XWidget()
	{
		m_spHostLable = TSP_Label::value_type::create();
		m_spHostLable->setAnchorPoint(cocos2d::Vec2(0, 0));
		m_spHostLable->setAlignment(cocos2d::TextHAlignment::LEFT, cocos2d::TextVAlignment::BOTTOM);
		this->addChild(m_spHostLable);
	}

	XWidget::~XWidget()
	{
	}

	bool XWidget::setAttribute(tinyxml2::XMLElement* pAttNodes)
	{
		XACTION_ASSERT(NULL != pAttNodes, return false;);
		XACTION_ASSERT(::IsStringEqual(XLocalT8("Property"), pAttNodes->Value()), return false;);

		std::wstring strName;
		strName = X::utility::__SafeGetXmlAttribute(pAttNodes, XLocalT16("name"), XLocalT16(""));

		std::wstring strValue;
		strValue = X::utility::__SafeGetXmlAttribute(pAttNodes, XLocalT16("value"), XLocalT16(""));
		 

		if (strName.empty() || strValue.empty() || 
			::IsStringEqual(strName, XLocalT16("")) || IsStringEqual(strValue, XLocalT16("")))
		{
			return false;
		}

		bool bRet = true;
		if (::IsStringEqual(strName, XLocalT16("AnchorPoint")))
		{
			auto pos = utility::LoadFromString<cocos2d::Vec2>(strValue);
			//this->ignoreAnchorPointForPosition(false);
			this->setAnchorPoint(pos);
		}
		else if (::IsStringEqual(strName, XLocalT16("Pos")))
		{
			auto pos = utility::LoadFromString<cocos2d::Vec2>(strValue);
			this->setPosition(pos);
		}
		else if (::IsStringEqual(strName, XLocalT16("Size")))
		{
			auto size = utility::LoadFromString<cocos2d::Size>(strValue);
			this->setContentSize(size);
		}
		else if (::IsStringEqual(strName, XLocalT16("Adaptive")))
		{

		}
		else if (::IsStringEqual(strName, XLocalT16("BkImage")))
		{
			std::string strPath;
			cocos2d::Rect srcRect;
			cocos2d::Rect tarRect;
			cocos2d::Rect scale9Rect;
			X::utility::ReadImagePropertyInfo(pAttNodes, strPath, srcRect, tarRect, scale9Rect);

			setBkImage(XU16TLocal(strValue), srcRect, scale9Rect);
		}
		else if (::IsStringEqual(strName, XLocalT16("BkColor")))
		{
			auto color4f = utility::LoadFromString<cocos2d::Color4B>(strValue);
			m_BkColor = color4f;
		}
		else if (::IsStringEqual(strName, XLocalT16("Text")))
		{
			setText(XU16T8(strValue));
		}
		else if (::IsStringEqual(strName, XLocalT16("TextColor")))
		{
			cocos2d::Color4B textColor = utility::LoadFromString<cocos2d::Color4B>(strValue);
			setTextColor(textColor);
		}
		else if (::IsStringEqual(strName, XLocalT16("TextFontSize")))
		{
			uint32_t nFontSize = 0;
			nFontSize = utility::LoadFromString<uint32_t>(strValue);
			setTextFontSize(nFontSize);
		}
		else
		{
			bRet = false;
		}

		if (false == bRet)
		{
			bRet =  __TSUPER::setAttribute(pAttNodes);
		}
		
		return bRet;
	}

	void XWidget::setBkImage(const std::string& strFileName)
	{
		setBkImage(X::CreateSprite(strFileName));
	}

	void XWidget::setBkImage(const std::string& strFileName, cocos2d::Rect& strRect)
	{
		setBkImage(X::CreateSprite(strFileName, strRect));
	}

	void XWidget::setBkImage(const std::string& strFileName, cocos2d::Rect& strRect,
		cocos2d::Rect& scale9Rect)
	{
		setBkImage(X::CreateSprite(strFileName, strRect, scale9Rect));
	}

	void XWidget::setBkImage(TSP_Scale9Sprite& spSpr)
	{
		if (nullptr != m_spBkSpr)
		{
			this->removeChild(m_spBkSpr);
		}

		m_spBkSpr = spSpr;
		m_spBkSpr->setPosition(getNodeSpaceCenterPos());
		m_spBkSpr->setName(XLocalT8("__xnode_internel_auto_BkImage"));
		this->addChild(m_spBkSpr);
	}

	void XWidget::setText(const std::string& strText)
	{
		m_strText = strText;
		__updateDateText();
	}

	std::string XWidget::getText()
	{
		return m_strText;
	}

	void XWidget::setTextColor(cocos2d::Color4B color)
	{
		if (m_spHostLable)
			m_spHostLable->setTextColor(color);
	}

	cocos2d::Color4B XWidget::getTextColor()
	{
		cocos2d::Color4B color;
		if (m_spHostLable)
			color = m_spHostLable->getTextColor();

		return color;
	}

	void XWidget::setTextFontSize(uint32_t nFontSize)
	{
		if (m_spHostLable)
			m_spHostLable->setSystemFontSize(nFontSize);

		__updateDateText();
	}

	int32_t XWidget::getTextFontSize()
	{
		uint32_t nFontSize = 0;
		if (m_spHostLable)
			nFontSize = m_spHostLable->getSystemFontSize();

		__updateDateText();

		return nFontSize;
	}

	void XWidget::setFocusable(bool bFocusable)
	{
		m_bEnableFocus = bFocusable;
	}

	bool XWidget::getFocusable()
	{
		return m_bEnableFocus;
	}

	void XWidget::setFocus(bool bFocus)
	{
		bool bOldStatus = m_bStatusFocused;
		m_bStatusFocused = bFocus;

		if (m_bStatusFocused)
			onGetFocus();
		else if ((true == bOldStatus) && (false == m_bStatusFocused))
			onLoseFocus();
	}

	bool XWidget::getFocus()
	{
		return m_bStatusFocused;
	}

	void XWidget::onMouseDown(cocos2d::EventMouse* event)
	{

	}

	void XWidget::onMouseUp(cocos2d::EventMouse* event)
	{
		if (m_bIsMouseIn)
		{
			setFocus(true);
			event->stopPropagation();
		}
		else
		{
			setFocus(false);
		}
	}

	void XWidget::onMouseMove(cocos2d::EventMouse* event)
	{
		cocos2d::Point posLocal = getNodeSpacePosFromMouseEvent(event);
		bool bContains = getNodeSpaceRect().containsPoint(posLocal);
		if (!bContains)
		{
			if (true == m_bIsMouseIn)
			{
				m_bIsMouseIn = false;
				onMouseOut(event);
			}

			return;
		}
		else
		{
			if (false == m_bIsMouseIn)
			{
				m_bIsMouseIn = true;
				onMouseIn(event);
			}
		}
	}

	void XWidget::onMouseScroll(cocos2d::EventMouse* event)
	{

	}

	void XWidget::onMouseIn(cocos2d::EventMouse* event)
	{

	}

	void XWidget::onMouseOut(cocos2d::EventMouse* event)
	{

	}

	void XWidget::onGetFocus()
	{

	}

	void XWidget::onLoseFocus()
	{

	}

	void XWidget::__updateDateText()
	{
		if (nullptr != m_spHostLable)
		{
			m_spHostLable->setString(m_strText);
		}
	}
}


